<?php
    require_once dirname(__FILE__) . '/dao_news.php';
    $dao_news = new dao_news();
    $error = "";
    $success = "";
    $id_news_hidden = 0;

    var_dump($_POST);

    // DELETE
    if (!empty($_GET['d_id'])) :
        $id = $_GET['d_id'];
        if ($dao_news->delete_news($id)) {
            $success = "Delete news successfully!";
            ?>
            <script type="text/javascript">
                window.location = "raw.php";
            </script>
        <?php
        }
    endif;

    // LOAD EDIT ITEM
    if (!empty($_GET['e_id']) && empty($_POST['btn_edit'])) {
        $id             = $_GET['e_id'];
        $edit_item      = $dao_news->find_news($id);
        $id_news_hidden = $_GET['e_id'];
        goto endscript;
    }

    // INSERT
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (!empty($_POST['btn_submit'])) :
            $txt_news_title    = trim($_POST['txt_news_title']);
            $txt_content  = trim($_POST['txt_content']);
            $ddl_category = $_POST['ddl_category'];

            if ($dao_news->add_news($txt_news_title, $txt_content, $ddl_category)) {
                $success = "Insert Successfully!";
                ?>
                <script type="text/javascript">
                    window.location = "raw.php";
                </script>
            <?php
            } else {
                $error = "Failed!";
            }
        endif;

        // EDIT
        if (!empty($_POST['btn_edit'])) {
            $id           = $_POST['id_hidden'];
            $txt_news_title    = trim($_POST['txt_news_title']);
            $txt_content  = trim($_POST['txt_content']);
            $ddl_category = $_POST['ddl_category'];

            if ($dao_news->update_news($id, $txt_news_title, $txt_content, $ddl_category)) {
                $success = "edit successfully!";
            } else {
                ?>
                <script type="text/javascript">
                    alert('Failed to edit!');
                    window.location = "raw.php";
                </script>
            <?php
            }
        }
    }


    endscript:
?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
    <link href="ckeditor/contents.css">
</head>
<body>
<div class="row">
    <div style="color: red"><?php echo $error; ?></div>
    <div style="color: green"><?php echo $success; ?></div>

    <form action="raw.php" name="form_news" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="id_hidden" id="id_hidden" value="<?php echo $id_news_hidden; ?>">

        <div class="form-group">
            <label for="txt_news_title" class="col-md-2 control-label">Title:</label>

            <div class="col-md-10">
                <input name="txt_news_title"
                       type="text"
                       required=""
                       value="<?php if (!empty($edit_item)) echo $edit_item['title']; ?>"
                       class="form-control"
                       placeholder="news title">
            </div>
        </div>
        <br/><br/>

        <div class="form-group">
            <label for="ddl_category" class="col-md-2 control-label">Category:</label>

            <div class="col-md-10">
                <select name="ddl_category" class="form-control">
                    <?php
                        $list_category = $dao_news->get_list_cat();
                        foreach ($list_category as $category) :
                            ?>
                            <option value="<?php echo $category['id'] ?>"
                                <?php if (!empty($edit_item)) {
                                    if ($category['id'] == $edit_item['cat_id']) {
                                        echo 'selected';
                                    }
                                } ?>>
                                <?php echo $category['name'] ?>
                            </option>
                        <?php
                        endforeach;
                    ?>
                </select>
            </div>
        </div>
        <br/><br/>

        <div class="form-group">
            <label for="txt_content" class="col-md-2 control-label">Content:</label>

            <div class="col-md-10">
                <textarea name="txt_content"
                          class="ckeditor"
                          required=""
                          class="form-control">
                    <?php if (!empty($edit_item)) echo $edit_item['content']; ?>
                </textarea>
            </div>
        </div>
        <br/><br/>

        <input name="btn_submit" type="submit" value="Insert" class="btn btn-primary">
        <input name="btn_edit" type="submit" value="Update" class="btn btn-danger">
    </form>

    <?php
        $list = $dao_news->get_list_news();
        if (!empty($list)):
            ?>
            <table class="table table-bordered" style="margin-top: 50px;">
                <tr>
                    <th>ID</th>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Content</th>
                    <th>Created Date</th>
                    <th>Modified Date</th>
                    <th colspan="2">Action</th>
                </tr>
                <?php
                    foreach ($list as $item) :
                        $cat = $dao_news->find_cat($item['cat_id']);
                        ?>
                        <tr>
                            <td><?php echo $item['id'] ?></td>
                            <td><?php echo $item['title'] ?></td>
                            <td><?php echo $cat['name'] ?></td>
                            <td>"<?php echo htmlspecialchars($item['content']); ?>"</td>
                            <td><?php echo $item['created_date'] ?></td>
                            <td><?php echo $item['modified_date'] ?></td>
                            <td><a href="raw.php?e_id=<?php echo $item['id'] ?>"><img
                                        src="icons/edit.png" alt="edit"
                                        style="height: 20px;"></a></td>
                            <td><a href="raw.php?d_id=<?php echo $item['id'] ?>"><img
                                        src="icons/delete.png"
                                        alt="delete"
                                        onclick="return confirm('Are you sure?')"
                                        style="height: 20px;"></a>
                            </td>
                        </tr>
                    <?php
                    endforeach;
                ?>
            </table>
        <?php endif; ?>
</div>
</body>
</html>
